package dingding.使用线程池;

import java.util.concurrent.*;

public class AsyncThreadPoolExample {

    private final static int AVALABLE_PROCESSORS = Runtime.getRuntime().availableProcessors();

    private final static ThreadPoolExecutor pool = new ThreadPoolExecutor(AVALABLE_PROCESSORS,
            AVALABLE_PROCESSORS*2, 1, TimeUnit.MINUTES,
            new LinkedBlockingDeque<>(5),
            new ThreadPoolExecutor.CallerRunsPolicy());

    public static String doSomethingA(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----doSomethingA");
        return "A Task Done";
    }

    public static String doSomethingB(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("-----doSomethingA");
        return "A Task Done";
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Long start = System.currentTimeMillis();
        Future<?> resultA = pool.submit(() -> doSomethingA());
        Long end = System.currentTimeMillis();
        System.out.println(end - start);
        Thread.sleep(5000);
        System.out.println(resultA.isDone());
        System.out.println(resultA.get());
    }
}
